package main

import (
	"fmt"
	"time"
)

func timeDemo() {
	//获取当前时间
	now := time.Now()
	fmt.Printf("current _time:%v\n", now)

	year := now.Year() //年 -> now对象的
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute() //分钟
	second := now.Second() //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

func timestampDemo() {
	//获取当前时间
	now := time.Now()
	//获取时间戳
	timestamp1 := now.Unix()
	//纳秒时间戳
	timestamp2 := now.UnixNano()
	fmt.Printf("current timestamp1:%v\n", timestamp1)
	fmt.Printf("current timestamp2:%v\n", timestamp2)
}

func timestampDemo2(timestamp int64) {
	//将时间戳转为时间格式
	timeObj := time.Unix(timestamp, 0)
	fmt.Println(timeObj)
	year := timeObj.Year()
	month := timeObj.Month()
	day := timeObj.Day()       //日
	hour := timeObj.Hour()     //小时
	minute := timeObj.Minute() //分钟
	second := timeObj.Second() //秒
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}

func work_overtime() {
	now := time.Now()
	fmt.Println(now)
	now_add := now.Add(time.Hour)
	fmt.Println(now_add)
}

// 解析时区
func timeZone() {
	now := time.Now()
	fmt.Println(now)
	// 加载时区
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println(err)
		return
	}
	// 按照指定时区和指定格式解析字符串时间
	timeObj, err := time.ParseInLocation("2006/01/02 15:04:05", "2019/08/04 14:15:20", loc)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(timeObj)
	fmt.Println(timeObj.Sub(now))
}

func main() {
	timeDemo()
	timestampDemo()
	timestampDemo2(time.Now().Unix())
	work_overtime()
}
